package com.shxc.smarttrade.old.presenter;

import android.os.Handler;

import com.shxc.smarttrade.old.bean.AccountInfo;
import com.shxc.smarttrade.old.bean.ForeignOrder;
import com.shxc.smarttrade.old.bean.PendOrder;
import com.shxc.smarttrade.old.bean.PositionOrderList;
import com.shxc.smarttrade.old.iview.IViewPosition;
import com.shxc.smarttrade.old.modal.IRequestServiceListener;
import com.shxc.smarttrade.old.modal.PositionModal;

/**
 * Created by jcdev1 on 2017/5/31.
 */

public class PositionPresenter {

    private PositionModal modal;
    private IViewPosition iViewPosition;
    private Handler handler = new Handler();


    public PositionPresenter(IViewPosition iViewPosition) {
        modal = new PositionModal();
        this.iViewPosition = iViewPosition;
    }

    /**
     * 持仓列表
     *
     * @param userId
     */
    public void getForeignOrder(String accessToken, int userId, final int tag) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (tag != 1)
                    iViewPosition.showDialog();
            }
        });
        modal.getForeignOrder(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.setForeignOrder((ForeignOrder) result);
                        iViewPosition.dismissDialog();
                        iViewPosition.dismissrefresh();

                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.dismissrefresh();
                        iViewPosition.showMyToast(msg);
                        iViewPosition.dismissDialog();
                        iViewPosition.showNetErrorLayout();
                    }
                });

            }
        });
    }

    /**
     * 挂单列表
     *
     * @param userId
     */
    public void getPendingList(String accessToken, int userId, final int tag) {
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                if (tag != 1)
//                    iViewPosition.showDialog();
//            }
//        });
        modal.getPendingList(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(Object result) {
                PendOrder pendOrder = (PendOrder) result;
                iViewPosition.setPendingList(pendOrder.getData().getPendOrder());
//                iViewPosition.dismissDialog();
                iViewPosition.dismissrefresh();
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.dismissrefresh();
                        iViewPosition.showMyToast(msg);
//                        iViewPosition.dismissDialog();
                    }
                });
            }
        });
    }

    /**
     * 获取账户基本信息
     *
     * @param accessToken
     * @param userId
     * @param tag
     */
    public void getAccountInfo(String accessToken, int userId, final int tag) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (tag != 1)
                    iViewPosition.showDialog();
            }
        });
        modal.getAccountInfo(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.setAccountInfo((AccountInfo) result);
                        iViewPosition.dismissDialog();
                        iViewPosition.dismissrefresh();

                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.dismissrefresh();
                        iViewPosition.showMyToast(msg);
                        iViewPosition.dismissDialog();
                        iViewPosition.showNetErrorLayout();
                    }
                });

            }
        });
    }

    /**
     * 获取持仓列表
     *
     * @param accessToken
     * @param userId
     * @param tag
     */
    public void getPositionOrderList(String accessToken, int userId, final int tag) {
//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                if (tag != 1)
//                    iViewPosition.showDialog();
//            }
//        });
        modal.getPositionOrderList(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewPosition.setPositionOrderList((PositionOrderList) result);
//                        iViewPosition.dismissDialog();
//                        iViewPosition.dismissrefresh();

                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                        iViewPosition.dismissrefresh();
                        iViewPosition.showMyToast(msg);
//                        iViewPosition.dismissDialog();
                        iViewPosition.showNetErrorLayout();
                    }
                });

            }
        });
    }
}
